<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				userCackeKey:"bro-th-returnApply-user-cache",
				acceptInstCacheKey:"bro-th-returnApply-acceptInstId"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				if(_that.edit){
					_that.om.formSave("returnApply/doUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("returnApply/doAdd",_that.formdata,_that)
				}
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:"common/findCategoryByBusinessCode",
						param:{
							businessCode:options.code
						}
					}
				]
				if(fileId){
					ajaxArr.push({
						url:'returnApply/getValue',
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("topinfo","businessCategoryId","data",configSelectData(res[1].value,'id','name'),mainData)
					changeEditor("title1",'userId',{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({
								posCanBeNull:false
							}),
							url:"orgInstPosUserOfficial/findPagerByOrgId",
							namefield:"userPosName",
							cachename:_that.userCackeKey,
							placeholder:"请输入姓名,岗位",
							searchfield:"q",
						},
					},null,mainData)
					changeEditor("title1","acceptInstId",{
						link: "../../public/search/search",
						"linkParam": {
							other: JSON.stringify({
								containBoard: true
							}),
							method: "post",
							namefield: 'instName',
							searchfield: "q",
							initload: true,
							url: 'tinstitution/findNotBoard1',
							cachename: _that.acceptInstCacheKey,
							placeholder: "请输入部门名称"
						}
					},null,mainData)
					if(_that.subConfigData["title4"]){
						let configData = _that.subConfigData["title4"]
						let att = {}
						if(hasEditor("title4","projectName",configData)){
							let title = getEditorTitle("title4","projectName",configData)
							att.titleTemplate = "<%= projectName || '未填写'%>"
						}
						changeEditor("title4","dataStr",att,mainData)
					}
					if(_that.subConfigData["title5"]){
						let configData = _that.subConfigData["title5"]
						let att = {}
						if(hasEditor("title5","userPostId",configData)){
							let title = getEditorTitle("title5","userPostId",configData)
							att.titleTemplate = "<%= userName || '未填写'%>"
						}
						changeEditor("title5","reviewerStr",att,mainData)
					}
					if(fileId){
						let fileIdArr = getFormFields(mainData)
						fileIdArr.push("id")
						for(let index in res[2].value){
							if(fileIdArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value[index])
								_that.$set(_that.formdata,index,res[2].value[index])
							}
						}
						let dataList = _that._.cloneDeep(res[2].value.dataList)
						_that.subEditorObj["title4"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
						_that.$set(_that.formdata,'dataStr',JSON.stringify(dataList))
						delete res[2].value.dataList
						
						let reviewerList = _that._.cloneDeep(res[2].value.reviewerList)
						_that.subEditorObj["title5"].workList = reviewerList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"reviewerStr",JSON.stringify(reviewerList))
						_that.$set(_that.formdata,'reviewerStr',JSON.stringify(reviewerList))
						delete res[2].value.reviewerList
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("topinfo","businessCategoryId")
						})
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload() {
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(let fieldId in _that.subEditorObj){
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key:getWorkCacheKey(fieldId),
						success:(res)=>{
							if(res.data){
								editor.workList = res.data
							}
							uni.removeStorage({
								key:getWorkCacheKey(fieldId)
							})
						}
					})
				}
				uni.getStorage({
					key:_that.userCackeKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userId = d.userId
							formdata.userPostId = d.userId
							formdata.userName = d.userName
							formdata.postId = d.posId
							formdata.postName = d.postName
							formdata.instId = d.instId
							formdata.instName = d.instName
							formdata.mobile = d.autoMobile
							uni.removeStorage({
								key:_that.userCackeKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.acceptInstCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.acceptInstId = d.id
							formdata.acceptInstName = d.instName
							uni.removeStorage({
								key:_that.acceptInstCacheKey
							})
						}
					}
				});
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
